<template>
      <el-dialog
      title="添加标签"
      :visible="showAddDialog"
      class="showEditDialog"
      style="width: 50%"
      @close="btnCancel"
    >
      <el-form :model="EditData" :rules="rules" ref="addForm" @close="btnEditCancel">
        <el-form-item label="所属学科" prop="subjectName" v-if="!isOK">
          <el-select
            v-model="EditData.subjectName"
            style="width: 80%"
            size="small"
          >
            <el-option
              v-for="item in subjectList"
              :key="item.id"
              :label="item.subjectName"
              :value="item.subjectName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="tagName">
          <el-input
            v-model="EditData.tagName"
            style="width: 80%"
            size="small"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-row slot="footer" style="text-align: right">
        <el-col>
            <el-button @click="btnCancel">取消</el-button>
            <el-button type="primary" @click="saveEdit()">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
</template>

<script>
import { add as addTag } from '@/api/hmmm/tags'
export default {
  props: {
    showAddDialog: {
      type: Boolean,
      required: true
    },
    subjectList: {
      type: [],
      required: true
    },
    EditData: {
      type: Object,
      required: true
    },
    rules: {
      type: Object,
      required: true
    },
    isOK: {
      type: Boolean,
      required: true
    }
  },
  data () {
    return {
    }
  },
  methods: {
    btnEditCancel () {

    },
    async saveEdit () {
      // console.log(this.subjectList)
      var addObject = {}
      this.subjectList.forEach(item => {
        if (item.subjectName === this.EditData.subjectName) {
          addObject.subjectID = item.id
          return addObject
        }
      })
      addObject.tagName = this.EditData.tagName
      // 校验数据
      await this.$refs.addForm.validate()
      // 新增接口
      await addTag(addObject)
      // 提示成功
      this.$message.success('新增成功')
      // 重新拉取数据
      this.$emit('loadTagtList')
      // 关闭弹层
      this.$emit('update:showAddDialog', false)
    },
    btnCancel () {
      // 重置表单
      this.$refs.addForm.resetFields()
      // 调用父组件的方法
      this.$parent.btnAddCancel()
    }
  }
}
</script>

<style scoped lang='scss'></style>
